﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Main_Inventory_WinSaleQty : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    int intControlCounter = 1;
    List<string> LstCounter;
    List<string> LstLocation;
    List<string> LstBatch;
    List<string> LstBalanceQty;
    List<string> LstSaleQty;
    List<string> LstWarranty;
    List<string> LstWarrantyDetails;
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        OverrideAddMore();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            LstCounter = new List<string>();
            ViewState["LstCounter"] = LstCounter;

            LstLocation = new List<string>();
            ViewState["LstLocation"] = LstLocation;

            LstBalanceQty = new List<string>();
            ViewState["LstBalanceQty"] = LstBalanceQty;

            LstSaleQty = new List<string>();
            ViewState["LstSaleQty"] = LstSaleQty;

            LstBatch = new List<string>();
            ViewState["LstBatch"] = LstBatch;

            LstWarranty = new List<string>();
            ViewState["LstWarranty"] = LstWarranty;

            LstWarrantyDetails = new List<string>();
            ViewState["LstWarrantyDetails"] = LstWarrantyDetails;            

            BindLocation(ddlLocation1);
            
            if (Request.QueryString["ItemId"] != null)
            {
                hidItemId.Value = Request.QueryString["ItemId"].ToString();
                EnableBatchLink(Request.QueryString["ItemId"].ToString());
                BindBatch(ddlBatch1);
                EnableExpiryDate(Request.QueryString["ItemId"].ToString());
            }
            if (Request.QueryString["CtrlId"] != null)
            {
                hidRequestCtrl.Value = Request.QueryString["CtrlId"].ToString();
                
            }            
            if (Request.QueryString["HidLocationId"] != null)
            {
                hidLocationCtrlId.Value = Request.QueryString["HidLocationId"].ToString();
            }
            if (Request.QueryString["LocationIdandQty"] != null)
            {
                hidLocationIdandQty.Value = Request.QueryString["LocationIdandQty"].ToString();                
                BindRequestData(hidLocationIdandQty.Value.ToString());
            }
            
            VisibleBatchColumn();
            VisibleWarrantyColumn();
        }
       
    }
    protected void AddMoreRow()
    {
        intControlCounter++;

        HtmlTableRow HTRow = new HtmlTableRow();

        HtmlTableCell HTCell1 = new HtmlTableCell();
        DropDownList ddlLocation = new DropDownList();
        ddlLocation.ID = "ddlLocation" + intControlCounter.ToString();
        ddlLocation.Width = Unit.Percentage(98);
        ddlLocation.CssClass = "dropdown";
        BindLocation(ddlLocation);
        //ddlLocation.Attributes.Add("onchange", "return checkItemRepetion(this.id);");
        HTCell1.Controls.Add(ddlLocation);
        LstCounter.Add(intControlCounter.ToString());
        LstLocation.Add(ddlLocation.ID);
        //onchange="return checkItemRepetion(this.id);"
        HtmlTableCell HTCellBatch = new HtmlTableCell();
        if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()))
        {           
            DropDownList ddlBatch = new DropDownList();
            ddlBatch.ID = "ddlBatch" + intControlCounter.ToString();
            ddlBatch.Width = Unit.Percentage(98);
            ddlBatch.CssClass = "dropdown";
            BindBatch(ddlBatch);
            //ddlBatch.Attributes.Add("onchange", "return checkItemRepetion(this.id);");
            HTCellBatch.Controls.Add(ddlBatch);
            LstBatch.Add(ddlBatch.ID);
        }


        HtmlTableCell HTCell2 = new HtmlTableCell();
        Label lblBalanceQty = new Label();
        lblBalanceQty.ID = "lblBalanceQty" + intControlCounter.ToString();
        lblBalanceQty.Width = Unit.Percentage(98);
        lblBalanceQty.CssClass = "labelNumber";
        HTCell2.Controls.Add(lblBalanceQty);
        LstBalanceQty.Add(lblBalanceQty.ID);

        HtmlTableCell HTCell3 = new HtmlTableCell();
        TextBox txtSaleQty = new TextBox();
        txtSaleQty.ID = "txtSaleQty" + intControlCounter.ToString();
        txtSaleQty.Width = Unit.Percentage(98);
        txtSaleQty.CssClass = "inputnumber";
        txtSaleQty.Text = "0.00";
        txtSaleQty.Attributes.Add("onfocus", "return RemoveZero(this.id);");
        txtSaleQty.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
        txtSaleQty.Attributes.Add("onblur", "return CalculateQty();");        
        HTCell3.Controls.Add(txtSaleQty);
        LstSaleQty.Add(txtSaleQty.ID);

        HtmlTableCell HTCell4 = new HtmlTableCell();
        HtmlTableCell HTCell5 = new HtmlTableCell();
        if (EnableWarranty(Request.QueryString["ItemId"].ToString()))
        {

            TextBox txtWarranty = new TextBox();
            txtWarranty.ID = "txtWarranty" + intControlCounter.ToString();
            txtWarranty.Width = Unit.Percentage(98);
            txtWarranty.CssClass = "inputnumber";            
            txtWarranty.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
            HTCell4.Controls.Add(txtWarranty);
            LstWarranty.Add(txtWarranty.ID);

            DropDownList ddlWarrantyDetails = new DropDownList();
            ddlWarrantyDetails.ID = "ddlWarrantyDetails" + intControlCounter.ToString();
            ddlWarrantyDetails.Width = Unit.Percentage(98);
            ddlWarrantyDetails.CssClass = "dropdown";
            ddlWarrantyDetails.Items.Add(new ListItem("Months", "1"));
            ddlWarrantyDetails.Items.Add(new ListItem("Years", "0"));
            HTCell5.Controls.Add(ddlWarrantyDetails);
            LstWarrantyDetails.Add(ddlWarrantyDetails.ID);
        }

        HTRow.Cells.Add(HTCell1);
        if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()))
        {
            HTRow.Cells.Add(HTCellBatch);
            ViewState["LstBatch"] = LstBatch;
        }
        HTRow.Cells.Add(HTCell2);
        HTRow.Cells.Add(HTCell3);
        if (EnableWarranty(Request.QueryString["ItemId"].ToString()))
        {
            HTRow.Cells.Add(HTCell4);
            HTRow.Cells.Add(HTCell5);

            ViewState["LstWarranty"] = LstWarranty;           
            ViewState["LstWarrantyDetails"] = LstWarrantyDetails;
        }
       
        ViewState["LstCounter"] = LstCounter;
        ViewState["LstLocation"] = LstLocation;
        ViewState["LstBalanceQty"] = LstBalanceQty;
        ViewState["LstSaleQty"] = LstSaleQty;

        tblItem.Rows.Add(HTRow);

        hidControlCounter.Value = intControlCounter.ToString();
       
    }
    protected void OverrideAddMore()
    {
        LstCounter = (List<string>)ViewState["LstCounter"];
        LstBatch = (List<string>)ViewState["LstBatch"];
        LstLocation = (List<string>)ViewState["LstLocation"];
        LstBalanceQty = (List<string>)ViewState["LstBalanceQty"];
        LstSaleQty = (List<string>)ViewState["LstSaleQty"];
        LstWarranty = (List<string>)ViewState["LstWarranty"];
        LstWarrantyDetails = (List<string>)ViewState["LstWarrantyDetails"];
        if (LstCounter != null)
        {
            int count = Convert.ToInt32(LstCounter.Count);
            for (int i = 0; i < count; i++)
            {
                intControlCounter++;

                HtmlTableRow HTRow = new HtmlTableRow();

                HtmlTableCell HTCell1 = new HtmlTableCell();
                DropDownList ddlLocation = new DropDownList();
                ddlLocation.ID = LstLocation[i].ToString();
                ddlLocation.Width = Unit.Percentage(98);
                ddlLocation.CssClass = "dropdown";
                BindLocation(ddlLocation);
               // ddlLocation.Attributes.Add("onchange", "return checkItemRepetion(this.id);");
                HTCell1.Controls.Add(ddlLocation);

                HtmlTableCell HTCellBatch = new HtmlTableCell();
                if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()))
                {
                   
                    DropDownList ddlBatch = new DropDownList();
                    ddlBatch.ID = LstBatch[i].ToString();
                    ddlBatch.Width = Unit.Percentage(98);
                    ddlBatch.CssClass = "dropdown";
                    BindBatch(ddlBatch);
                    ddlBatch.Attributes.Add("onchange", "return checkItemRepetion(this.id);");
                    HTCellBatch.Controls.Add(ddlBatch);
                }
                HtmlTableCell HTCell2 = new HtmlTableCell();
                Label lblBalanceQty = new Label();
                lblBalanceQty.ID = LstBalanceQty[i].ToString();
                lblBalanceQty.Width = Unit.Percentage(98);
                lblBalanceQty.CssClass = "labelNumber";
                HTCell2.Controls.Add(lblBalanceQty);

                HtmlTableCell HTCell3 = new HtmlTableCell();
                TextBox txtSaleQty = new TextBox();
                txtSaleQty.ID = LstSaleQty[i].ToString();
                txtSaleQty.Width = Unit.Percentage(98);
                txtSaleQty.CssClass = "inputnumber";
                txtSaleQty.Text = "0.00";
                txtSaleQty.Attributes.Add("onfocus", "return RemoveZero(this.id);");
                txtSaleQty.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
                txtSaleQty.Attributes.Add("onblur", "return CalculateQty();");                
                HTCell3.Controls.Add(txtSaleQty);

                HtmlTableCell HTCell4 = new HtmlTableCell();
                HtmlTableCell HTCell5 = new HtmlTableCell();
                if (EnableWarranty(Request.QueryString["ItemId"].ToString()))
                {

                    TextBox txtWarranty = new TextBox();
                    txtWarranty.ID = LstWarranty[i].ToString();
                    txtWarranty.Width = Unit.Percentage(98);
                    txtWarranty.CssClass = "inputnumber";
                    //txtWarranty.Attributes.Add("onfocus", "return RemoveZero(this.id);");
                    txtWarranty.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
                    HTCell4.Controls.Add(txtWarranty);
                    

                    DropDownList ddlWarrantyDetails = new DropDownList();
                    ddlWarrantyDetails.ID = LstWarrantyDetails[i].ToString();
                    ddlWarrantyDetails.Width = Unit.Percentage(98);
                    ddlWarrantyDetails.CssClass = "dropdown";
                    ddlWarrantyDetails.Items.Add(new ListItem("Months", "1"));
                    ddlWarrantyDetails.Items.Add(new ListItem("Years", "0"));
                    HTCell5.Controls.Add(ddlWarrantyDetails);
                    
                }



                HTRow.Cells.Add(HTCell1);
                if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()))
                {
                    HTRow.Cells.Add(HTCellBatch);                   
                }
                HTRow.Cells.Add(HTCell2);
                HTRow.Cells.Add(HTCell3);
                if (EnableWarranty(Request.QueryString["ItemId"].ToString()))
                {
                    HTRow.Cells.Add(HTCell4);
                    HTRow.Cells.Add(HTCell5);                    
                }
                tblItem.Rows.Add(HTRow);
            }
        }
        
    }
    protected void btnAddMore_Click(object sender, EventArgs e)
    {
        AddMoreRow();
        BindBatchAfterSave();        
    }
    protected void BindLocation(DropDownList ddl)
    {
        strsql = "Select WHSID,WHSLOCATION from IVR_WAREHOUSE";
        DataTable DT = DU.getdata(strsql);
        if(DT.Rows.Count>0)
        {
            ddl.DataSource = DT;
            ddl.DataTextField = "WHSLOCATION";
            ddl.DataValueField = "WHSID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("-Select location-", "0"));
        }
    }
    protected void BindBatch(DropDownList ddl)
    {
        if (ddl.Items.Count > 0)
        {
            string selectedValue = "0";
            if (ddl.SelectedValue != "0")
            {
                selectedValue = ddl.SelectedValue;
            }
            ddl.Items.Clear();
            SqlParameter[] sparam ={
                                   new SqlParameter ("@ITMID",Request.QueryString["ItemId"].ToString())
                               };
            DataTable DT = DU.getdatausingprocedure("USP_IVR_GetAllBatchNumbers", sparam);
            if (DT.Rows.Count > 0)
            {
                ddl.DataSource = DT;
                ddl.DataTextField = "BATNUM";
                ddl.DataValueField = "BATID";
                ddl.DataBind();
                ddl.SelectedValue = selectedValue.ToString();
            }
        }
        else
        {
            SqlParameter[] sparam ={
                                   new SqlParameter ("@ITMID",Request.QueryString["ItemId"].ToString())
                               };
            DataTable DT = DU.getdatausingprocedure("USP_IVR_GetAllBatchNumbers", sparam);
            if (DT.Rows.Count > 0)
            {
                ddl.DataSource = DT;
                ddl.DataTextField = "BATNUM";
                ddl.DataValueField = "BATID";
                ddl.DataBind();                
            }
        }
    }
    protected void BindRequestData(string LocationIdandQty)
    {
        if (LocationIdandQty.ToString() != string.Empty)
        {
            if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()) && EnableWarranty(Request.QueryString["ItemId"].ToString()))
            {
                List<string> lstLocationId = new List<string>();
                List<string> lstBatchId = new List<string>();
                List<string> lstLocationQty = new List<string>();
                List<string> lstWarranty1 = new List<string>();
                List<string> lstWarrantyDetails1 = new List<string>();

                if (LocationIdandQty.ToString().Contains("/") && LocationIdandQty.ToString().Contains("|") && LocationIdandQty.ToString().Contains(":"))
                {
                    string[] strMainArray = LocationIdandQty.ToString().Split('/');
                    foreach (string strSubArray in strMainArray)
                    {
                        string[] str = strSubArray.ToString().Split(',');
                        lstLocationId.Add(str[0].ToString());
                        string[] str1 = str[1].ToString().Split('|');
                        lstLocationQty.Add(str1[0].ToString());
                        string[] str2 = str1[1].ToString().Split(':');
                        lstBatchId.Add(str2[0].ToString());
                        string[] str3 = str2[1].ToString().Split(';');
                        lstWarranty1.Add(str3[0].ToString());
                        lstWarrantyDetails1.Add(str3[1].ToString());
                    }
                }
                else
                {
                    string[] str = LocationIdandQty.ToString().Split(',');
                    lstLocationId.Add(str[0].ToString());
                    string[] str1 = str[1].ToString().Split('|');
                    lstLocationQty.Add(str1[0].ToString());
                    string[] str2 = str1[1].ToString().Split(':');
                    lstBatchId.Add(str2[0].ToString());
                    string[] str3 = str2[1].ToString().Split(';');
                    lstWarranty1.Add(str3[0].ToString());
                    lstWarrantyDetails1.Add(str3[1].ToString());
                }
                if (lstLocationId != null)
                {
                    double dblTotal = 0;
                    int count = Convert.ToInt32(lstLocationId.Count);
                    if (count > 1)
                    {
                        for (int i = 1; i < count; i++)
                        {
                            AddMoreRow();
                        }
                    }
                    for (int i = 1; i <= count; i++)
                    {
                        DropDownList ddl = (DropDownList)tblItem.Rows[i].FindControl("ddlLocation" + i.ToString());
                        DropDownList ddlBatch = (DropDownList)tblItem.Rows[i].FindControl("ddlBatch" + i.ToString());
                        TextBox txtSale = (TextBox)tblItem.Rows[i].FindControl("txtSaleQty" + i.ToString());
                        TextBox txtWarranty = (TextBox)tblItem.Rows[i].FindControl("txtWarranty" + i.ToString());
                        DropDownList ddlWarrantyDetails = (DropDownList)tblItem.Rows[i].FindControl("ddlWarrantyDetails" + i.ToString());
                        ddl.SelectedValue = lstLocationId[i - 1].ToString();
                        ddlBatch.SelectedValue = lstBatchId[i - 1].ToString();
                        txtSale.Text = lstLocationQty[i - 1].ToString();
                        txtWarranty.Text = lstWarranty1[i - 1].ToString();
                        ddlWarrantyDetails.SelectedValue = lstWarrantyDetails1[i - 1].ToString();                        
                        dblTotal += Convert.ToDouble(txtSale.Text);
                    }
                    txtTotalQty.Text = string.Format("{0:0.00}", dblTotal);

                }
            }
            else if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()) && !EnableWarranty(Request.QueryString["ItemId"].ToString()))
            {
                List<string> lstLocationId = new List<string>();
                List<string> lstBatchId = new List<string>();
                List<string> lstLocationQty = new List<string>();

                if (LocationIdandQty.ToString().Contains("/") && LocationIdandQty.ToString().Contains("|"))
                {
                    string[] strMainArray = LocationIdandQty.ToString().Split('/');
                    foreach (string strSubArray in strMainArray)
                    {
                        string[] str = strSubArray.ToString().Split(',');
                        lstLocationId.Add(str[0].ToString());
                        string[] str1 = str[1].ToString().Split('|');
                        lstLocationQty.Add(str1[0].ToString());
                        lstBatchId.Add(str1[1].ToString());
                    }
                }
                else
                {                    
                    string[] str = LocationIdandQty.ToString().Split(',');
                    lstLocationId.Add(str[0].ToString());
                    string[] str1 = str[1].ToString().Split('|');
                    lstLocationQty.Add(str1[0].ToString());
                    lstBatchId.Add(str1[1].ToString());
                }

                if (lstLocationId != null)
                {
                    double dblTotal = 0;
                    int count = Convert.ToInt32(lstLocationId.Count);
                    if (count > 1)
                    {
                        for (int i = 1; i < count; i++)
                        {
                            AddMoreRow();
                        }
                    }
                    for (int i = 1; i <= count; i++)
                    {
                        DropDownList ddl = (DropDownList)tblItem.Rows[i].FindControl("ddlLocation" + i.ToString());
                        DropDownList ddlBatch = (DropDownList)tblItem.Rows[i].FindControl("ddlBatch" + i.ToString());
                        TextBox txtSale = (TextBox)tblItem.Rows[i].FindControl("txtSaleQty" + i.ToString());
                        ddl.SelectedValue = lstLocationId[i - 1].ToString();
                        ddlBatch.SelectedValue = lstBatchId[i - 1].ToString();
                        txtSale.Text = lstLocationQty[i - 1].ToString();
                        dblTotal += Convert.ToDouble(txtSale.Text);
                    }
                    txtTotalQty.Text = string.Format("{0:0.00}", dblTotal);

                }
            }
            else if (!EnableBatchColumns(Request.QueryString["ItemId"].ToString()) && !EnableWarranty(Request.QueryString["ItemId"].ToString()))
            {
                List<string> lstLocationId = new List<string>();
                List<string> lstLocationQty = new List<string>();

                if (LocationIdandQty.ToString().Contains("/"))
                {
                    string[] strMainArray = LocationIdandQty.ToString().Split('/');
                    foreach (string strSubArray in strMainArray)
                    {
                        string[] str = strSubArray.ToString().Split(',');
                        lstLocationId.Add(str[0].ToString());
                        lstLocationQty.Add(str[1].ToString());
                    }
                }
                else
                {
                    string[] str = LocationIdandQty.ToString().Split(',');
                    lstLocationId.Add(str[0].ToString());
                    lstLocationQty.Add(str[1].ToString());
                }

                if (lstLocationId != null)
                {
                    double dblTotal = 0;
                    int count = Convert.ToInt32(lstLocationId.Count);
                    if (count > 1)
                    {
                        for (int i = 1; i < count; i++)
                        {
                            AddMoreRow();
                        }
                    }
                    for (int i = 1; i <= count; i++)
                    {
                        DropDownList ddl = (DropDownList)tblItem.Rows[i].FindControl("ddlLocation" + i.ToString());
                        TextBox txtSale = (TextBox)tblItem.Rows[i].FindControl("txtSaleQty" + i.ToString());
                        ddl.SelectedValue = lstLocationId[i - 1].ToString();
                        txtSale.Text = lstLocationQty[i - 1].ToString();
                        dblTotal += Convert.ToDouble(txtSale.Text);
                    }
                    txtTotalQty.Text = string.Format("{0:0.00}", dblTotal);

                }
            }
        }
    }
    protected void EnableBatchLink(string ItemId)
    {
        strsql = "Select ITMBATCH from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            if (DT.Rows[0]["ITMBATCH"].ToString()=="True")
            {
                btnBatch.Visible = true;
                hidbatchEnabled.Value = "True";
            }
            else
            {
                hidbatchEnabled.Value = "False";
                btnBatch.Visible = false;
            }
        }
    }
    protected bool EnableBatchColumns(string ItemId)
    {
        bool Flag = true;
        if (ItemId.ToString() != string.Empty)
        {
            strsql = "Select ITMBATCH from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (DT.Rows[0]["ITMBATCH"].ToString()== "True")
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                }
            }
        }
        return Flag;
    }
    protected void VisibleBatchColumn()
    {
        if (EnableBatchColumns(Request.QueryString["ItemId"].ToString()))
        {
            tdBatch0.Visible = true;
            tdBatch1.Visible = true;
        }
        else
        {
            tdBatch0.Visible = false;
            tdBatch1.Visible = false;
        }
    }
    protected bool EnableWarranty(string ItemId)
    {
         bool Flag = true;
         if (ItemId.ToString() != string.Empty)
         {
             strsql = "Select ITMBATCH, ITMBATCHOPT from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
             DataTable DT = DU.getdata(strsql);
             if (DT.Rows.Count > 0)
             {
                 if (DT.Rows[0]["ITMBATCH"].ToString()=="True" && DT.Rows[0]["ITMBATCHOPT"].ToString() == "1")
                 {
                     Flag = true;
                     hidWarrantyEnabled.Value = "True";
                 }
                 else
                 {
                     Flag = false;
                     hidWarrantyEnabled.Value = "False";
                 }
             }
         }
         return Flag;
    }
    protected void VisibleWarrantyColumn()
    {
        if (EnableWarranty(Request.QueryString["ItemId"].ToString()))
        {
            tdWarranty0.Visible = true;
            tdWarranty1.Visible = true;
            tdWarrantyDetails0.Visible = true;
            tdWarrantyDetails1.Visible = true;
        }
        else
        {
            tdWarranty0.Visible = false;
            tdWarranty1.Visible = false;
            tdWarrantyDetails0.Visible = false;
            tdWarrantyDetails1.Visible = false;
        }
    }
    protected void EnableExpiryDate(string ItemId)
    {
        strsql = "Select ITMBATCH, ITMBATCHOPT from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0 && DT.Rows[0]["ITMBATCHOPT"].ToString() == "0")
            {
                trMFDDate.Visible = true;
                trExpiryDate.Visible = true;
                reqMFDDate.Enabled = true;
                reqExpiryDate.Enabled = true;
            }
            else
            {
                trMFDDate.Visible = false;
                trExpiryDate.Visible = false;
                reqMFDDate.Enabled = false;
                reqExpiryDate.Enabled = false;
            }
        }

    }
    protected bool ValidateBatchNumber()
    {
        bool Flag = true;
        SqlParameter[] sparam ={
                                   new SqlParameter("@ITMID",hidItemId.Value.ToString()),
                                   new SqlParameter("@BATNUM",txtBatchNo.Text.Trim())
                               };
        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetBatchID", sparam);
        if (DT.Rows.Count > 0)
        {
            lblMsgBatch.Text = "Duplicate Batch Number";
            Flag = false;
        }
        else
        {
            lblMsgBatch.Text = "";
            Flag = true;
        }
        return Flag;
    }
    protected void SaveBatchNumber()
    {

        if (ValidateBatchNumber())
        {
            int result = 0;
            strsql = "Select ITMBATCH, ITMBATCHOPT from IVR_ITEM Where ITMID='" + hidItemId.Value.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {

                if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0 && DT.Rows[0]["ITMBATCHOPT"].ToString() == "0")
                {
                    string strMFDDate = string.Empty;
                    string strExpiryDate = string.Empty;
                    if (txtMFDDate.Text != string.Empty)
                    {
                        string[] str = txtMFDDate.Text.ToString().Split('/');
                        strMFDDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
                    }
                    if (txtExpiryDate.Text != string.Empty)
                    {
                        string[] str = txtExpiryDate.Text.ToString().Split('/');
                        strExpiryDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
                    }
                    SqlParameter[] sparam ={
                                   new SqlParameter("@ITMID",hidItemId.Value.ToString()),
                                   new SqlParameter("@BATNUM",txtBatchNo.Text.Trim()),
                                   new SqlParameter("@BATMFGDATE",strMFDDate.ToString()),                                  
                                   new SqlParameter("@BATEXPIRYDATE",strExpiryDate.ToString()),
                                   new SqlParameter("@BATREMARKS",txtRemarks.Text.Trim())
                               };
                    result = DU.executeprocedure("USP_IVR_SaveBatchNumber", sparam);
                }
                else if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0 && DT.Rows[0]["ITMBATCHOPT"].ToString() != "0")
                {
                    SqlParameter[] sparam ={
                                   new SqlParameter("@ITMID",hidItemId.Value.ToString()),
                                   new SqlParameter("@BATNUM",txtBatchNo.Text.Trim()),                                   
                                   new SqlParameter("@BATREMARKS",txtRemarks.Text.Trim())
                               };
                    result = DU.executeprocedure("USP_IVR_SaveBatchNumber", sparam);
                }
                if (result > 0)
                {
                    txtBatchNo.Text = "";
                    txtExpiryDate.Text = "";
                    txtMFDDate.Text = "";
                    txtRemarks.Text = "";
                    lblMsgBatch.Text = "Batch Created";
                }
            }
        }

    }
    protected void btnSaveBatch_Click(object sender, EventArgs e)
    {
        SaveBatchNumber();
        BindBatchAfterSave();
        modalBatch.Show();
    }
    protected void BindBatchAfterSave()
    {
        if (EnableBatchColumns(hidItemId.Value.ToString()))
        {
            for (int i = 1; i < tblItem.Rows.Count; i++)
            {
                DropDownList ddl = (DropDownList)tblItem.Rows[i].FindControl("ddlBatch" + i.ToString());
                BindBatch(ddl);

            }
        }
    }
    protected void lnkBtnClose_Click(object sender, EventArgs e)
    {
        //lblMsgBatch.Text = "";
    }

}
                           
